﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace FDSimpleModelGenerator
{
    /// <summary>
    /// 模型验证
    /// </summary>
    public class ModelChecker
    {
        public static bool Check(ModelStateDictionary modelState, out string errMsg)
        {
            errMsg = string.Empty;
            if (modelState.IsValid)
            {
                return true;
            }

            foreach (var item in modelState.Values)
            {
                if (item.Errors.Count > 0)
                {
                    errMsg = item.Errors[0].ErrorMessage;
                    return false;
                }
            }

            return true;
        }
    }
}
